package lib

import (
	"GameServer/Common/data"
	"GameServer/Common/utils"
	"GameServer/Game/errors"
	"GameServer/Game/m"
	"strconv"

	log "github.com/sirupsen/logrus"
)

// 获取当前激活的活动，会检查时间有效性
func GetActActivityList() (ActList []data.ActActivity, err error) {
	//读表
	tpl, ok := data.GetAllDatas("ActActivities")
	if !ok {
		err = errors.NewErrcode(data.Errcode.ConfigNotFound, "没有找到激活活动配置,ActActivities")
		log.WithFields(log.Fields{
			"err":    err.Error(),
			"method": "GetActActivityList",
		}).Debug("没有找到激活活动配置,ActActivities")
		return nil, err
	}
	nowTime := utils.TNow()
	ActList = make([]data.ActActivity, 0)
	//fmt.Printf("----GetActActivityList---%d,%s\n", nowTime.Unix(), nowTime)
	for _, value := range tpl {
		actInfo := value.(data.ActActivity)
		beginTime, _ := utils.LayoutToTime(actInfo.BeginTime)
		endTime, _ := utils.LayoutToTime(actInfo.EndTime)
		if nowTime.Unix() >= beginTime.Unix() && nowTime.Unix() < endTime.Unix() {
			ActList = append(ActList, actInfo)
			//fmt.Printf("---InTime:actId:%d,beginTime = %s,endTime = %s \n", actInfo.ActivId, actInfo.BeginTime, actInfo.EndTime)
		} else {
			//fmt.Printf("NotInTime:actId:%d,beginTime = %s(%d),endTime = %s(%d) \n", actInfo.ActivId, actInfo.BeginTime, beginTime.Unix(), actInfo.EndTime, endTime.Unix())
		}
	}
	return ActList, nil
}

// 获取该活动在第几轮
func GetActTurn(actId int64) (actTurn int64, offsetTime int64) {
	actTurn = 0
	offsetTime = 0
	actLst, _ := GetActActivityList()
	if actLst == nil {
		return
	}
	for _, actAct := range actLst {
		if int64(actAct.ActivId) == actId {
			nowTime := utils.TNow().Unix()
			beginTime, _ := utils.LayoutToTime(actAct.BeginTime)
			offsetTime = nowTime - beginTime.Unix()
			if offsetTime <= 0 {
				offsetTime = 0
			}
			actTurn = (offsetTime) / (86400)
		}
	}
	actTurn = actTurn + 1
	return
}

func IsActActivityInActTime(actInfo *m.ActActivityInfo) (inTime bool) {
	inTime = false
	nowTime := utils.TNow().Unix()
	if nowTime >= actInfo.BeginTime && nowTime < actInfo.EndTime {
		inTime = true
	}
	return
}

// 该活动是否在激活时间内，从ActActivities表中获取
func IsActActivityInActTimeByActId(actId int64) (inTime bool) {
	//读表
	tpl, ok := data.GetAllDatas("ActActivities")
	if !ok {
		err := errors.NewErrcode(data.Errcode.ConfigNotFound, "没有找到激活活动配置,ActActivities")
		log.WithFields(log.Fields{
			"err":    err.Error(),
			"method": "IsActActivityInActTimeByActId",
		}).Debug("没有找到激活活动配置,ActActivities")
		return false
	}
	nowTime := utils.TNow()
	inTime = false
	for _, value := range tpl {
		actInfo := value.(data.ActActivity)
		if actId == int64(actInfo.ActivId) {
			beginTime, _ := utils.LayoutToTime(actInfo.BeginTime)
			endTime, _ := utils.LayoutToTime(actInfo.EndTime)
			if nowTime.After(beginTime) && nowTime.Before(endTime) {
				inTime = true
			}
		}
	}
	return inTime
}

func IsSlotAct(activId int64) (r bool) {
	_, ok := data.Get("SlotActivities", uint64(activId))
	return ok
}

func IsLoginAct(activId int64) (r bool) {
	_, ok := data.Get("LoginActActivities", uint64(activId))
	return ok
}

func IsOtherAct(activId int64) (r bool) {
	_, ok := data.Get("OthActivities", uint64(activId))
	return ok
}

func IsWorldBossAct(activId int64) (r bool) {
	_, ok := data.Get("WorldBossActivities", uint64(activId))
	return ok
}

func IsJackpotAct(activId int64) (r bool) {
	_, ok := data.Get("JackpotActivitys", uint64(activId))
	return ok
}

func IsEnergyQuickAct(activId int64) (r bool) {
	_, ok := data.Get("EnergyActivitys", uint64(activId))
	return ok
}

func IsChristmasAct(activId int64) (r bool) {
	_, ok := data.Get("ChristmasActivitys", uint64(activId))
	return ok
}

// 该ID活动属于什么类型，Slot老虎机，Login登录，Custom其他类型
func WhatIsActEnum(actId int64) (r int64) {
	slot := IsSlotAct(actId)
	login := IsLoginAct(actId)
	worldBoss := IsWorldBossAct(actId)
	jackpot := IsJackpotAct(actId)
	energy_quick := IsEnergyQuickAct(actId)
	christmas := IsChristmasAct(actId)
	if slot {
		r = data.ActivityEnum.Slot
	} else if login {
		r = data.ActivityEnum.Login
	} else if worldBoss {
		r = data.ActivityEnum.BossCombat
	} else if jackpot {
		r = data.ActivityEnum.Jackpot
	} else if energy_quick {
		r = data.ActivityEnum.EnergyQuick
	} else if christmas {
		r = data.ActivityEnum.Christmas
	} else {
		r = data.ActivityEnum.Custom
	}
	return r
}

// 获取该ID，老虎机活动类型，配置表内容
func GetSlotActConfigeTableInfo(activId int64) (r data.SlotActivity, err error) {
	tpl, ok := data.Get("SlotActivities", uint64(activId))
	if ok {
		r = tpl.(data.SlotActivity)
		return r, nil
	} else {
		err = errors.NewErrcode(data.Errcode.ConfigNotFound, "该活动不是老虎机活动,id : "+strconv.FormatInt(activId, 10))
		return
	}
}

// 获取该ID，登录活动类型，配置表内容
func GetLoginActConfigeTableInfo(activId int64) (r data.OtherActivity, err error) {
	tpl, ok := data.Get("LoginActActivities", uint64(activId))
	if ok {
		r = tpl.(data.OtherActivity)
		return r, nil
	} else {
		err = errors.NewErrcode(data.Errcode.ConfigNotFound, "该活动不是登录活动,id : "+strconv.FormatInt(activId, 10))
		return
	}
}

// 获取该ID，其他活动类型，配置表内容
func GetOtherActConfigeTableInfo(activId int64) (r data.OtherActivity, err error) {
	tpl, ok := data.Get("OthActivities", uint64(activId))
	if ok {
		r = tpl.(data.OtherActivity)
		return r, nil
	} else {
		err = errors.NewErrcode(data.Errcode.ConfigNotFound, "该活动不是其他活动,id : "+strconv.FormatInt(activId, 10))
		return
	}
}

// 获取该ID，世界BOSS活动类型，配置表内容
func GetWorldBossActConfigeTableInfo(activId int64) (r data.WorldBossActivity, err error) {
	tpl, ok := data.Get("WorldBossActivities", uint64(activId))
	if ok {
		r = tpl.(data.WorldBossActivity)
		return r, nil
	} else {
		err = errors.NewErrcode(data.Errcode.ConfigNotFound, "该活动不是世界BOSS活动,id : "+strconv.FormatInt(activId, 10))
		return
	}
}

// 获取该ID的世界BOSS累计伤害奖励
func GetBossDmgAccuBonus(configId int64) (r map[int64]data.BossDmgAccuBonus, err error) {
	tableName := "BossDmgAccuBonusLst" + strconv.FormatInt(configId, 10)
	tpl, ok := data.GetAllDatas(tableName)
	if ok {
		r = make(map[int64]data.BossDmgAccuBonus, 0)
		for _, value := range tpl {
			item := value.(data.BossDmgAccuBonus)
			r[int64(item.Id)] = item
		}
		return r, nil
	} else {
		err = errors.NewErrcode(data.Errcode.ConfigNotFound, "无法找到世界BOSS累计伤害奖励表："+tableName)
		return nil, err
	}
}

// 获取该ID的排名奖励
func GetActRankRewardList(rankRewardId int64) (r map[int64]data.ActivityRankReward, err error) {
	tableName := "ActivityRankReward" + strconv.FormatInt(rankRewardId, 10)
	tpl, ok := data.GetAllDatas(tableName)
	if ok {
		r = make(map[int64]data.ActivityRankReward, 0)
		for _, value := range tpl {
			item := value.(data.ActivityRankReward)
			r[int64(item.Rank)] = item
		}
		return r, nil
	} else {
		err = errors.NewErrcode(data.Errcode.ConfigNotFound, "无法找到活动奖励表："+tableName)
		return nil, err
	}
}

// 获取该ID的老虎机活动阶段奖励
func GetSlotActRewardList(rewardId int64) (r []data.SlotActivityReward, err error) {
	tableName := "SlotActivityReward" + strconv.FormatInt(rewardId, 10)
	tpl, ok := data.GetAllDatas(tableName)
	if ok {
		r = make([]data.SlotActivityReward, 100)
		for _, value := range tpl {
			item := value.(data.SlotActivityReward)
			r = append(r, item)
		}
		return r, nil
	} else {
		err = errors.NewErrcode(data.Errcode.ConfigNotFound, "无法找到老虎机活动阶段奖励表："+tableName)
		return nil, err
	}
}
